package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.mflex.WHSmartWhouseAd.BuildConfig;
import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.model.AuthorityModel;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;

import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.app.AppUtils;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.List;

public class  LoginActivity extends BaseActivity {

    private Button btnLogin;
    private TextView txtTitle;
    private EditText txtUser;
    private EditText txtPwd;

    private TextView txtVersion;
    private TextView txtApi;
    private ImageView passwordshow;
    MiniLoadingDialog mMiniLoadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "登录中...");
        txtTitle = (TextView) findViewById(R.id.txtTtitle);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        txtUser=(EditText)findViewById(R.id.txtUser);
        txtPwd=(EditText)findViewById(R.id.txtPwd);
        txtVersion=(TextView)findViewById(R.id.txtVersion);
        txtApi=(TextView)findViewById(R.id.txtApi);
        //txtTitle.setText(AppUtils.getAppName());
        SetListener();
        txtVersion.setText("程序版本" + AppUtils.getAppVersionName());
        txtApi.setText(BuildConfig.API_HOST);

        passwordshow=findViewById(R.id.passwordshow);
        passwordshow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               if(txtPwd.getTransformationMethod()==HideReturnsTransformationMethod.getInstance()) {
                   txtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                   txtPwd.setSelection(txtPwd.length());
               }else{
                   txtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                   txtPwd.setSelection(txtPwd.length());
               }

            }
        });
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }

    private void SetListener() {
        txtUser.requestFocus();
        //登录按钮监听
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StringUtils.isEmpty(txtUser.getText())){
                    XToastUtils.error("MES账号为空!", 2000);
                    return;
                }
                if (StringUtils.isEmpty(txtPwd.getText())) {
                    XToastUtils.error("MES密码为空!", 2000);
                    return;
                }
                request(v);

            }
        });
    }

    private void request(View v) {
        mMiniLoadingDialog.show();
        XHttp.get("api/WHSmartWhousePDA/CheckVersion")
                .params("Version", AppUtils.getAppVersionName())
                .syncRequest(false)
                .onMainThread(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<Boolean>() {
                    @Override
                    public void onSuccess(Boolean response) {
                        mMiniLoadingDialog.dismiss();

                        mMiniLoadingDialog.show();
                        XHttp.get("api/WHSmartWhousePDA/CheckLogin")
                                .params("LoginUser", txtUser.getText().toString())
                                .params("Pwd", txtPwd.getText().toString())
                                .syncRequest(false)
                                .onMainThread(true)
                                .timeOut(120000)
                                .execute(new SimpleCallBack<List<AuthorityModel>>() {
                                    @Override
                                    public void onSuccess(List<AuthorityModel> response) {
                                        mMiniLoadingDialog.dismiss();
                                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                        intent.putExtra("userName", txtUser.getText().toString());
                                        intent.putExtra("userPassword", txtPwd.getText().toString());
                                        intent.putExtra("auth", JsonUtil.toJson(response));
                                        startActivity(intent);
                                    }

                                    @Override
                                    public void onError(ApiException e) {
                                        mMiniLoadingDialog.dismiss();
                                        new MaterialDialog.Builder(v.getContext())
                                                .iconRes(R.drawable.icon_warning)
                                                .title(R.string.tip_warning)
                                                .content(e.getMessage())
                                                .positiveText(R.string.lab_submit)
                                                .show();
                                    }
                                });
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        new MaterialDialog.Builder(v.getContext())
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });


    }
    /**
     * 登录请求
     */
//    private void request(View v) {
//        mMiniLoadingDialog.show();
//
//        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//        intent.putExtra("userName", txtUser.getText().toString());
//        intent.putExtra("userPassword", txtPwd.getText().toString());
//        startActivity(intent);
//
//        XHttp.get("api/WHSmartWhousePDA/CheckLogin")
//                .params("LoginUser", txtUser.getText().toString())
//                .params("Pwd", txtPwd.getText().toString())
//                .syncRequest(false)
//                .onMainThread(true)
//                .timeOut(120000)
//                .execute(new SimpleCallBack<Object>() {
//                    @Override
//                    public void onSuccess(Object response) {
//                        mMiniLoadingDialog.dismiss();
//                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//                        intent.putExtra("userName", txtUser.getText().toString());
//                        intent.putExtra("userPassword", txtPwd.getText().toString());
//                        startActivity(intent);
//                    }
//
//                    @Override
//                    public void onError(ApiException e) {
//                        mMiniLoadingDialog.dismiss();
//                        ///XToastUtils.error(e.getMessage(),3000);
//                        new MaterialDialog.Builder(v.getContext())
//                                .iconRes(R.drawable.icon_warning)
//                                .title(R.string.tip_warning)
//                                .content(e.getMessage())
//                                .positiveText(R.string.lab_submit)
//                                .show();
//                    }
//                });
//    }

}
